package de.justphil.tcg.tcgserver.rest.db.mapstores;

import static de.justphil.tcg.tcgserver.commons.logging.TCGLogging.restLogger;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.TypedQuery;

import org.slf4j.Logger;

import com.hazelcast.core.MapStore;

import de.justphil.tcg.tcgserver.commons.domain.Game;
import de.justphil.tcg.tcgserver.commons.management.Application;
import de.justphil.tcg.tcgserver.rest.db.mapstores.helpers.LoadHelper;

public class GameMapStore implements MapStore<Long, Game> {

	private static final Logger log = restLogger(GameMapStore.class);
	
	private final EntityManagerFactory factory;

	public GameMapStore() {
		log.debug("New object created!");
		factory = Application.getEntityManagerFactory();
	}
	
	@Override
	public Game load(Long gameId) {
		log.debug("load()");
		
		EntityManager em = factory.createEntityManager();
		
		Game game = em.find(Game.class, gameId);
		loadTransientFields(em, game);
		
		em.close();
		
		return game;
	}

	@Override
	public Map<Long, Game> loadAll(Collection<Long> gameIds) {
		log.debug("loadAll()");
		
		EntityManager em = factory.createEntityManager();
		Map<Long, Game> gameMap = new HashMap<Long, Game>();
		
		List<Game> gameList = getGameList(em);
		for(Game game : gameList) {
			
			loadTransientFields(em, game);
			gameMap.put(game.getId(), game);
		}
		
		em.close();
		
		return gameMap;
	}

	@Override
	public Set<Long> loadAllKeys() {
		log.debug("loadAllKeys()");
		
		EntityManager em = factory.createEntityManager();
		
		Set<Long> gameIds = new HashSet<Long>();
		
		List<Game> gameList = getGameList(em);
		for (Game game : gameList) {
			gameIds.add(game.getId());
		}
		
		em.close();
		
		return gameIds;
	}
	
	private void loadTransientFields(EntityManager em, Game game) {
		if (game == null) {
			return;
		}
		
		LoadHelper.loadGamePlayers(em, game);
	}
	
	private List<Game> getGameList(EntityManager em) {
		TypedQuery<Game> q = em.createQuery("SELECT g FROM Game g ORDER BY g.id", Game.class);
		return q.getResultList();
	}

	@Override
	public void delete(Long gameId) {
		log.debug("delete()");
		
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		
		Game game = em.find(Game.class, gameId);
		if (game != null) {
			em.remove(game);
		}
		
		em.getTransaction().commit();
		em.close();
	}

	@Override
	public void deleteAll(Collection<Long> gameIds) {
		log.debug("deleteAll()");
		
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		
		Game game = null;
		for (Long gameId : gameIds) {
			game = em.find(Game.class, gameId);
			if (game != null) {
				em.remove(game);
			}
		}
		
		em.getTransaction().commit();
		em.close();
	}

	@Override
	public void store(Long gameId, Game game) {
		log.debug("store()");
		
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		
		Game pGame = em.find(Game.class, gameId);
		if (pGame == null) {
			log.debug("em.persist()");
			em.persist(game);
		}
		else {
			log.debug("em.merge()");
			em.merge(game);
		}
		
		em.getTransaction().commit();
		em.close();
	}

	@Override
	public void storeAll(Map<Long, Game> gameMap) {
		log.debug("storeAll()");
		
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		
		Game pGame	= null;
		Game game	= null;
		for (Long gameId : gameMap.keySet()) {
			game = gameMap.get(gameId);
			
			pGame = em.find(Game.class, gameId);
			if (pGame == null) {
				log.debug("em.persist()");
				em.persist(game);
			}
			else {
				log.debug("em.merge()");
				em.merge(game);
			}
		}
		
		em.getTransaction().commit();
		em.close();
	}

}
